package course

import (
	"net/http"

	"fmt"

	"awfan.cn/utils"
	"gopkg.in/mgo.v2"
	"gopkg.in/mgo.v2/bson"
)

func query(format, uid string, w http.ResponseWriter, db *mgo.Database) {

	if !bson.IsObjectIdHex(uid) {
		utils.WriteError(format, 405, fmt.Sprintf("#uid is wrong format: '%v'", uid), w)
		return
	}

	c := db.C(utils.PkgName(COURSE_XML{}))

	v := COURSES_XML{}
	if e := c.Find(bson.M{"member.id": uid}).All(&v.Courses); e != nil {
		utils.WriteError(format, 404, e.Error(), w)
		return
	}

	utils.WriteData(format, v, w)
}
